﻿#include "mainwidget.h"
#include <QtConcurrent/QtConcurrent>
MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent),
      m_exit(false),
      m_status(false)
{
    setWindowTitle("SoundR");
    setUi();
    getDevice();
    createTray();

    connect(m_btStart,&QPushButton::clicked,[this](){
        //关闭
        if(m_status)
        {
            if(m_audio_device.close())
            {
                m_status = false;
                m_exit = true;
                m_btStart->setText(tr("Start"));
                return;
            }
            else
            {
                QMessageBox::information(this,tr("error"),tr("Close fail!"));
                return;
            }
        }
        //开启
        if(m_audio_device.open(m_cbDevice->currentIndex()))
        {
            m_status = true;
            m_exit = false;
            m_btStart->setText(tr("Close"));
            QtConcurrent::run(QThreadPool::globalInstance(),this,&MainWidget::captureAudio);
        }
        else
        {
            QMessageBox::information(this,
                                     tr("error"),
                                     tr("Open fail!"));
        }
    });
    connect(&m_server,&Server::signalConnect,[this](){
        m_lbStatus->setText(tr("conneted"));
    });
    connect(&m_server,&Server::signalDisconnect,[this](){
        m_lbStatus->setText(tr("Not connected"));
    });
}

void MainWidget::captureAudio()
{
    uint nextpacketsize = 0;
    char* buf = nullptr;
    int size = 0;
    for(;;)
    {
        if(m_exit)
            return;
        m_audio_device.getBuffer(&buf,size,&nextpacketsize);
        if(buf == nullptr || size == 0 || nextpacketsize == 0)
        {
            QThread::msleep(1);
            continue;
        }
        if(buf)
            m_server.writeData(buf,size);
    }
}

MainWidget::~MainWidget()
{
    m_trayIcon->hide();
}

void MainWidget::closeEvent(QCloseEvent *event)
{
    if(m_cbCloseSelect->currentIndex() == 0)
    {
        m_exit = true;
    }
    else if(m_cbCloseSelect->currentIndex() == 1)
    {
        hide();
        event->ignore();
    }
}



void MainWidget::setUi()
{
    m_mainlayout = new QVBoxLayout(this);
    setLayout(m_mainlayout);

    m_btStart = new QPushButton(tr("Start"),this);
    m_lbStatus = new QLabel(tr("Not connected"),this);

    m_devicelayout = new QVBoxLayout();
    m_lbDevice = new QLabel(tr("Device:"),this);
    m_cbDevice = new QComboBox(this);
    m_cbDevice->setMinimumWidth(200);
    m_devicelayout->addWidget(m_lbDevice);
    m_devicelayout->addWidget(m_cbDevice);

    m_closelayout = new QHBoxLayout();
    m_lbCloseSelect = new QLabel(tr("Close:"),this);
    m_cbCloseSelect = new QComboBox(this);
    m_cbCloseSelect->addItem(tr("Close"));
    m_cbCloseSelect->addItem(tr("Tray"));
    m_closelayout->addWidget(m_lbCloseSelect);
    m_closelayout->addWidget(m_cbCloseSelect);

    m_mainlayout->addWidget(m_lbStatus);
    m_mainlayout->addLayout(m_closelayout);
    m_mainlayout->addLayout(m_devicelayout);
    m_mainlayout->addWidget(m_btStart);


}

void MainWidget::getDevice()
{
    m_cbDevice->clear();
    m_device_list = m_audio_device.list();

    for(int i = 0;i < m_device_list.size();++i)
    {
        m_cbDevice->addItem(m_device_list.at(i));
    }
}

void MainWidget::createTray()
{
    m_minimizeAction = new QAction(tr("Minimize"), this);
    connect(m_minimizeAction, &QAction::triggered, this, &QWidget::hide);

    m_restoreAction = new QAction(tr("Restore"), this);
    connect(m_restoreAction, &QAction::triggered, this, &QWidget::showNormal);

    m_quitAction = new QAction(tr("Quit"), this);
    connect(m_quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);

    m_trayIconMenu = new QMenu(this);
    m_trayIconMenu->addAction(m_minimizeAction);
    m_trayIconMenu->addAction(m_restoreAction);
    m_trayIconMenu->addSeparator();
    m_trayIconMenu->addAction(m_quitAction);

    m_trayIcon = new QSystemTrayIcon(this);
    m_trayIcon->setContextMenu(m_trayIconMenu);
    m_trayIcon->setIcon(QIcon(":/ico/logo"));
    m_trayIcon->show();
}
